/*
 * @Author: 0x9DEFA478
 * @Date: 2021-07-24 18:15:23
 * @LastEditTime: 2022-06-08 17:51:29
 * @LastEditors: 0x9DEFA478
 * @Description: 提供了一些位图绘制方法
 * QQ:2652450237
 * ============================================================================================================================================
 * 
 * 
 * 
 *                                                                                               ************      ****************************
 *                                                                                             ************      ****************************  
 *                                                                                           ************      ****************************    
 *                                                                                         ************      ****************************      
 *                                                                                       ************      ************                        
 *                                                                                     ************      ************                          
 *                                                                                   ************      ************                            
 *                                                                                 ************      ************                              
 *                                                                               ************      ************                                
 *                                                                             ************      ************                                  
 *                                                                           ************      ************                                    
 *                                                                         ************      ************                                      
 *                                                                       ************      ************                                        
 *                                                                     ************      ************                                          
 *                                                                   ************      ************                                            
 *                                                                 ************      ************                                              
 *                                                               ************      ************                                                
 *                                                             ************      ************                                                  
 *                                                           ************      ************                                                    
 *                                                         ************      ************                                                      
 *                                                       ************      ************                                                        
 *                                                     ************      ************                                                          
 *                                                   ************      ************                                                            
 *                                                 ************      ************                                                              
 *                                               ************      ************                                                                
 *                                             ************      ************                                                                  
 *                                           ************      ************                                                                    
 *                                         ************      ************                                                                      
 *                                       ************      ************                                                                        
 *                                     ************      ************                                                                          
 *                                   ************      ************                                                                            
 *                                 ************      ************                                                                              
 *                               ************      ************                                                                                
 *                             ************      ************                                                                                  
 *                           ************      ************                                                                                    
 *                         ************      ************                                                                                      
 *       ****************************      ************                                                                                        
 *     ****************************      ************                                                                                          
 *   ****************************      ************                                                                                            
 * ****************************      ************                                                                                              
 * 
 * 
 * 
 * ============================================================================================================================================
 * 
 */
#include "H_Graphics.h"


#define __WeakFunction __attribute__((weak))




/**
 * @brief 填充颜色 低位靠近原点垂直分布
 * @param Bitmap 要填充的位图
 * @param Area 操作区域
 * @param Color 颜色 0:黑色 其他:白色
 * @return 无
 */
__WeakFunction void H_Graphics_L1Vertical_Fill(H_Graphics_Bitmap* Bitmap,H_Graphics_Area* Area,H_Graphics_Color Color){
  H_Graphics_Axis dx;
  H_Graphics_Axis dy;
  H_Graphics_Axis ox;
  H_Graphics_Axis mx;
  H_Graphics_Axis oy;
  H_Graphics_Axis Height;
  union{
    H_Graphics_Axis dob;
    Hbyte dmsk;
  }dparam;
  H_Graphics_Axis tb;
  Hbyte* tDAddr;

  Height=Area->Height;
  
  dx=Area->X;
  dy=Area->Y;
  oy=0;
  while(oy<Height){

    Hbyte msk;

    tDAddr=Bitmap->Bitmap;

    tDAddr=&tDAddr[dx + (dy/8)*Bitmap->Width];

    //两个位图的字节位移
    dparam.dob=dy%8;

    //计算本次x循环复制的y方向的bit数
    tb=8-dparam.dob;
    if((oy+tb)>Height){
      tb=Height-oy;
    }

    msk=(0x01U<<tb)-1U;

    //计算掩码
    dparam.dmsk=msk<<dparam.dob;

    mx=Area->Width;
    ox=0;
    if(Color.L1==0U){
      dparam.dmsk=~dparam.dmsk;
      for (ox=0;ox<mx;ox++){
        tDAddr[ox]=tDAddr[ox]&dparam.dmsk;
      }
    }else{
      for(ox=0;ox<mx;ox++){
        tDAddr[ox]=tDAddr[ox]|dparam.dmsk;
      }
    }

    oy+=tb;
    dy+=tb;
  }
}

/**
 * @brief 图形复制 低位靠近原点垂直分布 转 低位靠近原点垂直分布 (OLED)
 * @param DstBitmap 要复制到的位图
 * @param SrcBitmap 源位图
 * @param Param 操作的参数
 * @return 无
 */
__WeakFunction void H_Graphics_L1Vertical_CopyTo_L1Vertical(H_Graphics_Bitmap* DstBitmap,H_Graphics_Bitmap* SrcBitmap,H_Graphics_Area_Param* Param){
  H_Graphics_Axis sx;
  H_Graphics_Axis sy;
  H_Graphics_Axis dx;
  H_Graphics_Axis dy;
  H_Graphics_Axis ox;
  H_Graphics_Axis mx;
  H_Graphics_Axis oy;
  union{
    H_Graphics_Axis sob;
    Hbyte smsk;
  }sparam;
  union{
    H_Graphics_Axis dob;
    Hbyte dmsk;
  }dparam;
  H_Graphics_Axis tb;
  Hbyte* tSAddr;
  Hbyte* tDAddr;
  
  dx=Param->DstX;
  dy=Param->DstY;
  sx=Param->SrcX;
  sy=Param->SrcY;
  oy=0;
  while(oy<Param->Height){

    Hsize dShift;
    Hbyte msk;

    tDAddr=DstBitmap->Bitmap;
    tSAddr=SrcBitmap->Bitmap;

    tDAddr=&tDAddr[dx + (dy/8)*DstBitmap->Width];
    tSAddr=&tSAddr[sx + (sy/8)*SrcBitmap->Width];

    //两个位图的字节位移
    dparam.dob=dy%8;
    sparam.sob=sy%8;

    //计算本次x循环复制的y方向的bit数
    if(sparam.sob>dparam.dob){
      tb=8-sparam.sob;
    }else{
      tb=8-dparam.dob;
    }
    if((oy+tb)>Param->Height){
      tb=Param->Height-oy;
    }

    msk=(0x01U<<tb)-1U;

    //计算相对位移(src要左移多少)
    dShift=dparam.dob-sparam.sob;

    //计算掩码
    sparam.smsk=msk<<sparam.sob;
    dparam.dmsk=~(msk<<dparam.dob);

    mx=Param->Width;
    ox=0;
    if(dShift<0){
      dShift=-dShift;

      for(ox=0;ox<mx;ox++){
        tDAddr[ox]=(tDAddr[ox]&dparam.dmsk)|((tSAddr[ox]&sparam.smsk)>>dShift);
      }
    }else if(dShift==0){
      for(ox=0;ox<mx;ox++){
        tDAddr[ox]=(tDAddr[ox]&dparam.dmsk)|(tSAddr[ox]&sparam.smsk);
      }
    }else{
      for(ox = 0;ox<mx;ox++){
        tDAddr[ox]=(tDAddr[ox]&dparam.dmsk)|((tSAddr[ox]&sparam.smsk)<<dShift);
      }
    }

    oy+=tb;
    dy+=tb;
    sy+=tb;
  }
}

/**
 * @brief 图形复制 低位靠近原点垂直分布 转 低位靠近原点垂直分布 反色 (OLED)
 * @param DstBitmap 要复制到的位图
 * @param SrcBitmap 源位图
 * @param Param 操作的参数
 * @return 无
 */
__WeakFunction void H_Graphics_L1Vertical_CopyTo_L1Vertical_Reverse(H_Graphics_Bitmap* DstBitmap,H_Graphics_Bitmap* SrcBitmap,H_Graphics_Area_Param* Param){
  H_Graphics_Axis sx;
  H_Graphics_Axis sy;
  H_Graphics_Axis dx;
  H_Graphics_Axis dy;
  H_Graphics_Axis ox;
  H_Graphics_Axis mx;
  H_Graphics_Axis oy;
  union{
    H_Graphics_Axis sob;
    Hbyte smsk;
  }sparam;
  union{
    H_Graphics_Axis dob;
    Hbyte dmsk;
  }dparam;
  H_Graphics_Axis tb;
  Hbyte* tSAddr;
  Hbyte* tDAddr;
  
  dx=Param->DstX;
  dy=Param->DstY;
  sx=Param->SrcX;
  sy=Param->SrcY;
  oy=0;
  while(oy<Param->Height){

    Hsize dShift;
    Hbyte msk;

    tDAddr=DstBitmap->Bitmap;
    tSAddr=SrcBitmap->Bitmap;

    tDAddr=&tDAddr[dx + (dy/8)*DstBitmap->Width];
    tSAddr=&tSAddr[sx + (sy/8)*SrcBitmap->Width];

    //两个位图的字节位移
    sparam.sob=sy%8;
    dparam.dob=dy%8;

    //计算本次x循环复制的y方向的bit数
    if(sparam.sob>dparam.dob){
      tb=8-sparam.sob;
    }else{
      tb=8-dparam.dob;
    }
    if((oy+tb)>Param->Height){
      tb=Param->Height-oy;
    }

    msk=(0x01U<<tb)-1U;

    //计算相对位移(src要左移多少)
    dShift=dparam.dob-sparam.sob;

    //计算掩码
    sparam.smsk=msk<<sparam.sob;
    dparam.dmsk=~(msk<<dparam.dob);

    mx=Param->Width;
    ox=0;
    if(dShift<0){
      dShift=-dShift;

      for (ox = 0;ox<mx;ox++){
        tDAddr[ox]=(tDAddr[ox]&dparam.dmsk)|(((~tSAddr[ox])&sparam.smsk)>>dShift);
      }
    }else if(dShift==0){
      for(ox=0;ox<mx;ox++){
        tDAddr[ox]=(tDAddr[ox]&dparam.dmsk)|((~tSAddr[ox])&sparam.smsk);
      }
    }else{
      for(ox=0;ox<mx;ox++){
        tDAddr[ox]=(tDAddr[ox]&dparam.dmsk)|(((~tSAddr[ox])&sparam.smsk)<<dShift);
      }
    }

    oy+=tb;
    dy+=tb;
    sy+=tb;
  }

}




/**
 * @brief 填充颜色
 * @param Bitmap 要填充的位图
 * @param Area 操作区域
 * @param Color 颜色
 * @return 无
 */
__WeakFunction void H_Graphics_RGB565_Fill(H_Graphics_Bitmap* Bitmap,H_Graphics_Area* Area,H_Graphics_Color Color){
  Huint16 color;
  Huint16* tDAddr;
  H_Graphics_Axis DLineOffset;
  H_Graphics_Axis W;
  H_Graphics_Axis Height;
  Hbyte Alpha;
  Hbyte rAlpha;


  Height=Area->Height;

  tDAddr=Bitmap->Bitmap;
  W=Bitmap->Width;
  tDAddr=&tDAddr[Area->X + Area->Y*W];
  DLineOffset=W - Area->Width;

  Alpha=Color.ARGB8888.A;
  if(Alpha==0xFFU){

#if vH_Graphics_IsBigend
    color=((((Huint16)Color.ARGB8888.R)<<8)&0xF800U)|((((Huint16)Color.ARGB8888.G)<<3)&0x07E0U)|((((Huint16)Color.ARGB8888.B)>>3)&0x001FU);
    color=(color>>8)|(color<<8);
#else
    color=((((Huint16)Color.ARGB8888.R)<<8)&0xF800U)|((((Huint16)Color.ARGB8888.G)<<3)&0x07E0U)|((((Huint16)Color.ARGB8888.B)>>3)&0x001FU);
#endif

    while(Height>0){
      Height--;

      W=Area->Width;
      while(W>8){
        W-=8;
        tDAddr[0]=color;
        tDAddr[1]=color;
        tDAddr[2]=color;
        tDAddr[3]=color;
        tDAddr[4]=color;
        tDAddr[5]=color;
        tDAddr[6]=color;
        tDAddr[7]=color;
        tDAddr=&tDAddr[8];
      }
      while(W>0){
        W--;
        tDAddr[0]=color;
        tDAddr=&tDAddr[1];
      }
      tDAddr=&tDAddr[DLineOffset];
    }

  }else{
    Huint16 DR;
    Huint16 DG;
    Huint16 DB;
    Huint16 SR;
    Huint16 SG;
    Huint16 SB;

    rAlpha=0xFFU-Alpha;

    SR=Color.ARGB8888.R;
    SG=Color.ARGB8888.G;
    SB=Color.ARGB8888.B;

    SR=SR*Alpha;
    SG=SG*Alpha;
    SB=SB*Alpha;

    while(Height>0){
      Height--;

      W=Area->Width;
      while(W>0){
        W--;

        color=tDAddr[0];
#if vH_Graphics_IsBigend
        color=(color>>8)|(color<<8);
#endif
        DR=(color&0xF800U)>>8;
        DG=(color&0x07E0U)>>3;
        DB=(color&0x001FU)<<3;

        DR=(DR*rAlpha+SR)/256U;
        DG=(DG*rAlpha+SG)/256U;
        DB=(DB*rAlpha+SB)/256U;

        tDAddr[0]=((DR<<8)&0xF800U)|((DG<<3)&0x07E0U)|((DB>>3)&0x001FU);

        tDAddr=&tDAddr[1];
      }
      tDAddr=&tDAddr[DLineOffset];
    }
  }

}

/**
 * @brief 图形复制 RGB565 转 RGB565
 * @param DstBitmap 要复制到的位图
 * @param SrcBitmap 源位图
 * @param Param 操作的参数
 * @return 无
 */
__WeakFunction void H_Graphics_RGB565_CopyTo_RGB565(H_Graphics_Bitmap* DstBitmap,H_Graphics_Bitmap* SrcBitmap,H_Graphics_Area_Param* Param){
  Huint16* tDAddr;
  H_Graphics_Axis DLineOffset;
  Huint16* tSAddr;
  H_Graphics_Axis SLineOffset;
  H_Graphics_Axis W;
  H_Graphics_Axis Width;
  H_Graphics_Axis Height;

  Width=Param->Width;
  Height=Param->Height;

  tDAddr=DstBitmap->Bitmap;
  W=DstBitmap->Width;
  tDAddr=&tDAddr[Param->DstX + Param->DstY*W];
  DLineOffset=W-Width;

  tSAddr=SrcBitmap->Bitmap;
  W=SrcBitmap->Width;
  tSAddr=&tSAddr[Param->SrcX + Param->SrcY*W];
  SLineOffset=W-Width;

  while(Height>0){
    Height--;

    W=Width;
    while(W>0){
      W-=8;
      tDAddr[0]=tSAddr[0];
      tDAddr[1]=tSAddr[1];
      tDAddr[2]=tSAddr[2];
      tDAddr[3]=tSAddr[3];
      tDAddr[4]=tSAddr[4];
      tDAddr[5]=tSAddr[5];
      tDAddr[6]=tSAddr[6];
      tDAddr[7]=tSAddr[7];
      tDAddr=&tDAddr[8];
      tSAddr=&tSAddr[8];
    }
    while(W>0){
      W--;
      tDAddr[0]=tSAddr[0];
      tDAddr=&tDAddr[1];
      tSAddr=&tSAddr[1];
    }
    tDAddr=&tDAddr[DLineOffset];
    tSAddr=&tSAddr[SLineOffset];
  }

}

/**
 * @brief 图形复制 RGB565带透明度 转 RGB565
 * @param DstBitmap 要复制到的位图
 * @param SrcBitmap 源位图
 * @param Param 操作的参数
 * @param Alpha 源图像透明度
 * @return 无
 */
__WeakFunction void H_Graphics_AlphaRGB565_CopyTo_RGB565(H_Graphics_Bitmap* DstBitmap,H_Graphics_Bitmap* SrcBitmap,H_Graphics_Area_Param* Param,Hbyte Alpha){
  Huint16 color;
  Huint16* tDAddr;
  H_Graphics_Axis DLineOffset;
  Huint16* tSAddr;
  H_Graphics_Axis SLineOffset;
  H_Graphics_Axis W;
  H_Graphics_Axis Width;
  H_Graphics_Axis Height;
  Huint16 DR;
  Huint16 DG;
  Huint16 DB;
  Huint16 SR;
  Huint16 SG;
  Huint16 SB;
  Hbyte rAlpha;

  Width=Param->Width;
  Height=Param->Height;

  tDAddr=DstBitmap->Bitmap;
  W=DstBitmap->Width;
  tDAddr=&tDAddr[Param->DstX + Param->DstY*W];
  DLineOffset=W-Width;

  tSAddr=SrcBitmap->Bitmap;
  W=SrcBitmap->Width;
  tSAddr=&tSAddr[Param->SrcX + Param->SrcY*W];
  SLineOffset=W-Width;

  rAlpha=0xFFU-Alpha;

  while(Height>0){
    Height--;

    W=Width;
    while(W>0){
      W--;

      color=tDAddr[0];
#if vH_Graphics_IsBigend
      color=(color>>8)|(color<<8);
#endif
      DR=(color&0xF800U)>>8;
      DG=(color&0x07E0U)>>3;
      DB=(color&0x001FU)<<3;
      color=tSAddr[0];
#if vH_Graphics_IsBigend
      color=(color>>8)|(color<<8);
#endif
      SR=(color&0xF800U)>>8;
      SG=(color&0x07E0U)>>3;
      SB=(color&0x001FU)<<3;

      DR=(DR*rAlpha+SR*Alpha)/256U;
      DG=(DG*rAlpha+SG*Alpha)/256U;
      DB=(DB*rAlpha+SB*Alpha)/256U;

      tDAddr[0]=((DR<<8)&0xF800U)|((DG<<3)&0x07E0U)|((DB>>3)&0x001FU);

      tDAddr=&tDAddr[1];
      tSAddr=&tSAddr[1];
    }
    tDAddr=&tDAddr[DLineOffset];
    tSAddr=&tSAddr[SLineOffset];
  }

}

/**
 * @brief 图形复制 低位靠近原点水平分布 转 RGB565
 * @param DstBitmap 要复制到的位图
 * @param SrcBitmap 源位图
 * @param Color 源位图颜色
 * @param Param 操作的参数
 * @return 无
 */
__WeakFunction void H_Graphics_L1_CopyTo_RGB565(H_Graphics_Bitmap* DstBitmap,H_Graphics_Bitmap* SrcBitmap,H_Graphics_Color Color,H_Graphics_Area_Param* Param){
  Huint16 color;
  Huint16* tDAddr;
  H_Graphics_Axis DLineOffset;
  Hbyte* SAddr;
  Hbyte* tSAddr;
  H_Graphics_Axis SLineBytes;
  H_Graphics_Axis SrcShift;
  H_Graphics_Axis W;
  H_Graphics_Axis Width;
  H_Graphics_Axis Height;
  Hbyte Alpha;
  Hbyte rAlpha;
  Hbyte sData;
  Hbyte Mask;

  Width=Param->Width;
  Height=Param->Height;

  tDAddr=DstBitmap->Bitmap;
  W=DstBitmap->Width;
  tDAddr=&tDAddr[Param->DstX + Param->DstY*W];
  DLineOffset=W-Width;

  tSAddr=SrcBitmap->Bitmap;
  W=SrcBitmap->Width;
  SLineBytes=W/8;
  W=Param->SrcX/8;
  tSAddr=&tSAddr[W + Param->SrcY*SLineBytes];
  SrcShift=Param->SrcX-W*8;

  Alpha=Color.ARGB8888.A;
  if(Alpha==0xFFU){

#if vH_Graphics_IsBigend
    color=((((Huint16)Color.ARGB8888.R)<<8)&0xF800U)|((((Huint16)Color.ARGB8888.G)<<3)&0x07E0U)|((((Huint16)Color.ARGB8888.B)>>3)&0x001FU);
    color=(color>>8)|(color<<8);
#else
    color=((((Huint16)Color.ARGB8888.R)<<8)&0xF800U)|((((Huint16)Color.ARGB8888.G)<<3)&0x07E0U)|((((Huint16)Color.ARGB8888.B)>>3)&0x001FU);
#endif

    while(Height>0){
      Height--;


      SAddr=tSAddr;

      if(SrcShift!=0){

        sData=SAddr[0];
        Mask=0x01U<<SrcShift;
        while(Mask!=0U){

          if((sData&Mask)!=0U){
            tDAddr[0]=color;
          }

          tDAddr=&tDAddr[1];
          Mask<<=1;
        }
        SAddr=&SAddr[1];

        W=Width-SrcShift;
      }else{
        W=Width;
      }

      while(W>7){
        W-=8;

        Mask=0x01U;
        sData=SAddr[0];
        while(Mask!=0U){
          
          if((sData&Mask)!=0U){
            tDAddr[0]=color;
          }

          tDAddr=&tDAddr[1];
          Mask<<=1;
        }
        SAddr=&SAddr[1];

      }

      Mask=0x01U;
      sData=SAddr[0];
      while(W>0){
        W--;

        if((sData&Mask)!=0U){
          tDAddr[0]=color;
        }

        tDAddr=&tDAddr[1];
        Mask<<=1;
      }


      tDAddr=&tDAddr[DLineOffset];
      tSAddr=&tSAddr[SLineBytes];
    }

  }else{
    Huint16 DR;
    Huint16 DG;
    Huint16 DB;
    Huint16 SR;
    Huint16 SG;
    Huint16 SB;

    rAlpha=0xFFU-Alpha;

    SR=Color.ARGB8888.R;
    SG=Color.ARGB8888.G;
    SB=Color.ARGB8888.B;

    SR=SR*Alpha;
    SG=SG*Alpha;
    SB=SB*Alpha;

    while(Height>0){
      Height--;


      SAddr=tSAddr;

      if(SrcShift!=0){

        sData=SAddr[0];
        Mask=0x01U<<SrcShift;
        while(Mask!=0U){

          if((sData&Mask)!=0U){
            color=tDAddr[0];
#if vH_Graphics_IsBigend
            color=(color>>8)|(color<<8);
#endif
            DR=(color&0xF800U)>>8;
            DG=(color&0x07E0U)>>3;
            DB=(color&0x001FU)<<3;

            DR=(DR*rAlpha+SR)/256U;
            DG=(DG*rAlpha+SG)/256U;
            DB=(DB*rAlpha+SB)/256U;

            tDAddr[0]=((DR<<8)&0xF800U)|((DG<<3)&0x07E0U)|((DB>>3)&0x001FU);
          }

          tDAddr=&tDAddr[1];
          Mask<<=1;
        }
        SAddr=&SAddr[1];

        W=Width-SrcShift;
      }else{
        W=Width;
      }

      while(W>7){
        W-=8;

        Mask=0x01U;
        sData=SAddr[0];

        while(Mask!=0U){
          
          if((sData&Mask)!=0U){
            color=tDAddr[0];
#if vH_Graphics_IsBigend
            color=(color>>8)|(color<<8);
#endif
            DR=(color&0xF800U)>>8;
            DG=(color&0x07E0U)>>3;
            DB=(color&0x001FU)<<3;

            DR=(DR*rAlpha+SR)/256U;
            DG=(DG*rAlpha+SG)/256U;
            DB=(DB*rAlpha+SB)/256U;

            tDAddr[0]=((DR<<8)&0xF800U)|((DG<<3)&0x07E0U)|((DB>>3)&0x001FU);
          }

          tDAddr=&tDAddr[1];
          Mask<<=1;
        }
        SAddr=&SAddr[1];

      }

      Mask=0x01U;
      sData=SAddr[0];
      while(W>0){
        W--;

        if((sData&Mask)!=0U){
          color=tDAddr[0];
#if vH_Graphics_IsBigend
          color=(color>>8)|(color<<8);
#endif
          DR=(color&0xF800U)>>8;
          DG=(color&0x07E0U)>>3;
          DB=(color&0x001FU)<<3;

          DR=(DR*rAlpha+SR)/256U;
          DG=(DG*rAlpha+SG)/256U;
          DB=(DB*rAlpha+SB)/256U;

          tDAddr[0]=((DR<<8)&0xF800U)|((DG<<3)&0x07E0U)|((DB>>3)&0x001FU);
        }

        tDAddr=&tDAddr[1];
        Mask<<=1;
      }


      tDAddr=&tDAddr[DLineOffset];
      tSAddr=&tSAddr[SLineBytes];
    }

  }

}

/**
 * @brief 图形复制 低位靠近原点水平分布 转 RGB565
 * @param DstBitmap 要复制到的位图
 * @param SrcBitmap 源位图
 * @param Color 源位图颜色
 * @param Param 操作的参数
 * @return 无
 */
__WeakFunction void H_Graphics_A4_CopyTo_RGB565(H_Graphics_Bitmap* DstBitmap,H_Graphics_Bitmap* SrcBitmap,H_Graphics_Color Color,H_Graphics_Area_Param* Param){
  Huint16 color;
  Huint16* tDAddr;
  H_Graphics_Axis DLineOffset;
  Hbyte* SAddr;
  Hbyte* tSAddr;
  H_Graphics_Axis SLineBytes;
  H_Graphics_Axis SrcShift;
  H_Graphics_Axis W;
  H_Graphics_Axis Width;
  H_Graphics_Axis Height;
  Huint16 DR;
  Huint16 DG;
  Huint16 DB;
  Huint16 SR;
  Huint16 SG;
  Huint16 SB;
  Hbyte Alpha;
  Hbyte alpha;
  Hbyte ralpha;
  Hbyte sData;
  Hbyte Mask;

  Width=Param->Width;
  Height=Param->Height;

  tDAddr=DstBitmap->Bitmap;
  W=DstBitmap->Width;
  tDAddr=&tDAddr[Param->DstX + Param->DstY*W];
  DLineOffset=W-Width;

  tSAddr=SrcBitmap->Bitmap;
  W=SrcBitmap->Width;
  SLineBytes=W/2;
  W=Param->SrcX/2;
  tSAddr=&tSAddr[W + Param->SrcY*SLineBytes];
  SrcShift=(Param->SrcX-W*2)*4;

  Alpha=Color.ARGB8888.A;
  

  SR=Color.ARGB8888.R;
  SG=Color.ARGB8888.G;
  SB=Color.ARGB8888.B;

  while(Height>0){
    Height--;


    SAddr=tSAddr;

    if(SrcShift!=0){

      sData=SAddr[0];
      Mask=0x0FU<<SrcShift;
      W=SrcShift;
      while(Mask!=0U){

        alpha=sData&Mask;
        if(alpha!=0U){
          alpha>>=W;

          alpha=((Huint16)alpha*Alpha)/16U;
          ralpha=0xFFU-alpha;

          color=tDAddr[0];
#if vH_Graphics_IsBigend
          color=(color>>8)|(color<<8);
#endif
          DR=(color&0xF800U)>>8;
          DG=(color&0x07E0U)>>3;
          DB=(color&0x001FU)<<3;

          DR=(DR*ralpha+SR*alpha)/256U;
          DG=(DG*ralpha+SG*alpha)/256U;
          DB=(DB*ralpha+SB*alpha)/256U;

          tDAddr[0]=((DR<<8)&0xF800U)|((DG<<3)&0x07E0U)|((DB>>3)&0x001FU);
        }
        W+=4;

        tDAddr=&tDAddr[1];
        Mask<<=4;
      }
      SAddr=&SAddr[1];

      W=Width-SrcShift;
    }else{
      W=Width;
    }

    while(W>1){
      W-=2;

      Mask=0x0FU;
      sData=SAddr[0];
      SrcShift=0;
      while(Mask!=0U){
        
        alpha=sData&Mask;
        if(alpha!=0U){
          alpha>>=SrcShift;

          alpha=((Huint16)alpha*Alpha)/16U;
          ralpha=0xFFU-alpha;

          color=tDAddr[0];
#if vH_Graphics_IsBigend
          color=(color>>8)|(color<<8);
#endif
          DR=(color&0xF800U)>>8;
          DG=(color&0x07E0U)>>3;
          DB=(color&0x001FU)<<3;

          DR=(DR*ralpha+SR*alpha)/256U;
          DG=(DG*ralpha+SG*alpha)/256U;
          DB=(DB*ralpha+SB*alpha)/256U;

          tDAddr[0]=((DR<<8)&0xF800U)|((DG<<3)&0x07E0U)|((DB>>3)&0x001FU);
        }

        tDAddr=&tDAddr[1];
        Mask<<=4;
        SrcShift+=4;
      }
      SAddr=&SAddr[1];

    }

    Mask=0x0FU;
    sData=SAddr[0];
    SrcShift=0;
    while(W>0){
      W--;

      alpha=sData&Mask;
      if(alpha!=0U){
        alpha>>=SrcShift;

        alpha=((Huint16)alpha*Alpha)/16U;
        ralpha=0xFFU-alpha;


        color=tDAddr[0];
#if vH_Graphics_IsBigend
        color=(color>>8)|(color<<8);
#endif
        DR=(color&0xF800U)>>8;
        DG=(color&0x07E0U)>>3;
        DB=(color&0x001FU)<<3;

        DR=(DR*ralpha+SR*alpha)/256U;
        DG=(DG*ralpha+SG*alpha)/256U;
        DB=(DB*ralpha+SB*alpha)/256U;

        tDAddr[0]=((DR<<8)&0xF800U)|((DG<<3)&0x07E0U)|((DB>>3)&0x001FU);
      }

      tDAddr=&tDAddr[1];
      Mask<<=4;
      SrcShift+=4;
    }


    tDAddr=&tDAddr[DLineOffset];
    tSAddr=&tSAddr[SLineBytes];
  }
    
}

/**
 * @brief 图形复制 8Bit掩膜 转 RGB565
 * @param DstBitmap 要复制到的位图
 * @param SrcBitmap 源位图
 * @param Color 源位图颜色
 * @param Param 操作的参数
 * @return 无
 */
__WeakFunction void H_Graphics_A8_CopyTo_RGB565(H_Graphics_Bitmap* DstBitmap,H_Graphics_Bitmap* SrcBitmap,H_Graphics_Color Color,H_Graphics_Area_Param* Param){
  Huint16 color;
  Huint16* tDAddr;
  H_Graphics_Axis DLineOffset;
  Hbyte* tSAddr;
  H_Graphics_Axis SLineOffset;
  H_Graphics_Axis W;
  H_Graphics_Axis Width;
  H_Graphics_Axis Height;
  Huint16 DR;
  Huint16 DG;
  Huint16 DB;
  Huint16 SR;
  Huint16 SG;
  Huint16 SB;
  Hbyte Alpha;
  Hbyte alpha;
  Hbyte ralpha;

  Width=Param->Width;
  Height=Param->Height;

  tDAddr=DstBitmap->Bitmap;
  W=DstBitmap->Width;
  tDAddr=&tDAddr[Param->DstX + Param->DstY*W];
  DLineOffset=W-Width;

  tSAddr=SrcBitmap->Bitmap;
  W=SrcBitmap->Width;
  tSAddr=&tSAddr[Param->SrcX + Param->SrcY*W];
  SLineOffset=W-Width;

  Alpha=Color.ARGB8888.A;
  
  SR=Color.ARGB8888.R;
  SG=Color.ARGB8888.G;
  SB=Color.ARGB8888.B;

  while(Height>0){
    Height--;

    W=Width;
    while(W>0){
      W--;

      color=tDAddr[0];
#if vH_Graphics_IsBigend
      color=(color>>8)|(color<<8);
#endif
      DR=(color&0xF800U)>>8;
      DG=(color&0x07E0U)>>3;
      DB=(color&0x001FU)<<3;

      alpha=tSAddr[0];
      alpha=((Huint16)alpha*Alpha)/256U;
      ralpha=0xFFU-alpha;

      DR=(DR*ralpha+SR*alpha)/256U;
      DG=(DG*ralpha+SG*alpha)/256U;
      DB=(DB*ralpha+SB*alpha)/256U;

      tDAddr[0]=((DR<<8)&0xF800U)|((DG<<3)&0x07E0U)|((DB>>3)&0x001FU);

      tDAddr=&tDAddr[1];
      tSAddr=&tSAddr[1];
    }
    tDAddr=&tDAddr[DLineOffset];
    tSAddr=&tSAddr[SLineOffset];
  }

}



